package com.example.probardemo;

//import android.R;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
ProgressBar progress=null;
Button btn=null;
int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.probar);

        progress=(ProgressBar) findViewById(R.id.progressBar1);
        btn=(Button) findViewById(R.id.button1);
        
        btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				progress.setVisibility(View.VISIBLE);
				updateBarHander.post(updateThread);
			}
		});
    }
   
    Handler updateBarHander=new Handler(){
    	public void handlerMessage (Message msg){  
    		progress.setProgress(i);  
            updateBarHander.post(updateThread);  
              
        }   
    };
    
Runnable updateThread=new Runnable() {
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("Begin Thread");
		i=i+10;
	    Message	msg=updateBarHander.obtainMessage(); 
	    msg.arg1=i;
	    
	    try{
	    Thread.sleep(1000);
	    }catch(InterruptedException e){
	    	e.printStackTrace();
	    }
	    updateBarHander.sendMessage(msg);
	    if(i==100){
	    	updateBarHander.removeCallbacks(updateThread);
	    }
	}
};   
}
